package com.module.base.pop.base

import android.content.Context
import android.view.LayoutInflater
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner
import com.kingja.loadsir.core.LoadService
import com.module.base.base.BaseExceptionViewModel
import com.module.base.base.adapter.inflateBindingWithGeneric
import com.module.base.ext.observe
import com.module.frame.base.BaseFrameViewModel
import com.module.frame.ext.loadDialogShareFlow
import com.module.frame.ext.postEvent
import java.lang.reflect.ParameterizedType

/**封装能含有mvvm的DrawerPopupView*/
abstract class BaseDrawerPopupView<VM : BaseExceptionViewModel, DB : ViewDataBinding>(context: Context) :
    com.lxj.xpopup.core.DrawerPopupView(context), ViewModelStoreOwner, LoadSirReloadPopupImpl {

    //绑定ViewModel
    abstract fun bingViewModel()

    abstract fun initData()

    open lateinit var mViewModel: VM
    lateinit var mDatabind: DB

    val mViewLifecycleOwner by lazy {
        LifecycleOwner { lifecycle }
    }
    private val mViewModelStore by lazy {////定义ViewModelStore变量
        ViewModelStore()
    }

    override fun addInnerContent() {
        try {
            mDatabind = try {
                inflateBindingWithGeneric(
                    1,
                    mViewLifecycleOwner,
                    LayoutInflater.from(context),
                    drawerContentContainer,
                    false
                )
            } catch (e: Exception) {
                DataBindingUtil.inflate(
                    LayoutInflater.from(context),
                    implLayoutId,
                    drawerContentContainer,
                    false
                )
            }
            drawerContentContainer.addView(mDatabind.root)
        } catch (e: Exception) {
            //如果都报错，则代表没有使用mDatabind,使用传统的
            super.addInnerContent()
        }

    }

    override fun onCreate() {
        super.onCreate()
        //vm初始化
        mViewModel = createViewModel()
        bingViewModel()
        initLoad()
        initListener()
        post { initData() }
    }

    open fun initListener() {
        /**
         * 加载框监听
         */
        observe(mViewModel.loadDialogShareFlow) {
            //发送全局
            postEvent(loadDialogShareFlow, it)
        }
    }

    override fun destroy() {
        super.destroy()
        viewModelStore.clear()
    }

    /**
     * 创建viewModel
     */
    private fun createViewModel(): VM {
        return try {
            val mClass =
                (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<VM>
            ViewModelProvider(this).get(mClass)
        } catch (e: Exception) {
            BaseFrameViewModel() as VM
        }
    }

    override fun getViewModelStore(): ViewModelStore {
        return mViewModelStore
    }

    override var loadService: LoadService<Any>? = null

    override fun isFinishing(): Boolean {
        return isDismiss
    }
}